package com.ismarkendenis.dxkeyexchange;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Reader 
{
	public byte[] readFile(String inputFileName)
	{
		File file     = new File(inputFileName);
		byte[] result = new byte[(int)file.length()];
		
		try 
		{
			InputStream input = null;
			
			try 
			{
				int totalBytesRead = 0;
				input = new BufferedInputStream(new FileInputStream(file));
				
				while(totalBytesRead < result.length)
				{
					int bytesRemaining = result.length - totalBytesRead;
					int bytesRead      = input.read(result, totalBytesRead, bytesRemaining); 
					
				  	if (bytesRead > 0)
				  	{
				  		totalBytesRead = totalBytesRead + bytesRead;
					}
				}
			}
			finally 
			{
			    input.close();
		    }
		}
	    catch (FileNotFoundException ex) 
	    {
	      System.out.println("File not found.");
	    }
	    catch (IOException ex) 
	    {
	      System.out.println(ex);
	    }
	    
	    return result;
	}
}